package com.base.cn.platform.os.controller.web.op.article;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleTypeService;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台咨询Controller
 * @author zsw
 * @since 20118/5/24
 */
@Controller
@RequestMapping("/web/article")
public class WebArticleController extends WebBaseController {

    @Autowired
    private OpArticleTypeService articleTypeService;
    @Autowired
    private OpGroomDataService groomDataService;
    @Autowired
    private OpArticleService articleService;
    @Autowired
    private PageViewCountService pageViewCountService;

    /**
     * 跳转到咨询首页
     * @param model
     * @return
     */
    @RequestMapping("/articleIndex")
    public String toArticleIndex(Model model){
        //查询资讯首页头条
        Map<String, Object> topArticle = new HashMap<>();
        topArticle.put("dataType","ARTICLE");
        topArticle.put("placeKey","AIHRE");
        List<Map<String, Object>> _topArticle = groomDataService.queryGroomDataList(topArticle);
        model.addAttribute("topArticle",_topArticle);

        //查询推荐资讯
        Map<String, Object> groomArticle = new HashMap<>();
        groomArticle.put("dataType","ARTICLE");
        groomArticle.put("placeKey","AIARE");
        List<Map<String, Object>> _groomArticle = groomDataService.queryGroomDataList(groomArticle);
        model.addAttribute("groomArticle",_groomArticle);

        //查询热门资讯
        Map<String, Object> hotArticle = new HashMap<>();
        hotArticle.put("dataType","ARTICLE");
        hotArticle.put("placeKey","APAH");
        List<Map<String, Object>> _hotArticle = groomDataService.queryGroomDataList(hotArticle);
        model.addAttribute("hotArticle",_hotArticle);
        pageViewCountService.addPageCount(PageKeyUtils.CMS_INDEX, PageFrom.PAGE_FROM_PC);
        return resultPage("/op/article/article-index");
    }

    /**
     * 新闻资讯详情
     * @param model
     * @param articleId
     * @return
     */
    @RequestMapping("/articleInfoById")
    public String  articleInfoById(Model model ,
                                   @RequestParam("articleId")BigDecimal articleId){
        Map<String, Object> article = articleService.findArticleById(articleId, true, 5);
        model.addAttribute("article",article);
        model.addAttribute("mobilePathShare", "/mobile/article/articleInfoById?articleId="+articleId);
        return resultPage("/op/article/article-info");
    }

    /**
     * 新闻资讯列表
     * @param model
     * @return
     */
    @RequestMapping("/toArticleList")
    public String  toArticleList(Model model){
        Map<String, Object> params = new HashMap<>();
        params.put("status",1);
        List<Map<String, Object>> articleType = articleTypeService.queryAllArticleType(params);
        model.addAttribute("dataMap",articleType);
        return resultPage("/op/article/article-list");
    }

    /**
     * 新闻资讯ajax列表
     * @return
     */
    @RequestMapping("/ajax/articleList")
    public String  ajaxArticleList(Model model,
                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                   @RequestBody Map<String, Object> article){
        article.put("status",0);
        article.put("releaseStatus",2);
        article.put("order",2);
        Map<String, Object> articleList = articleService.queryArticleList(currentPage,article);
        model.addAttribute("dataMap",articleList);
        return resultPage("/op/article/article-ajax-list");
    }

}
